package com.lanyun.controller;

import com.lanyun.pojo.Dept;
import com.lanyun.pojo.Emp;
import com.lanyun.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;
    @RequestMapping("/getAllAll")
    public String getAll(Model model){
        List<Dept> depts = deptService.getAll();
        model.addAttribute("count",depts.size());
        model.addAttribute("depts",depts);
        return "Dept";
    }
    @RequestMapping("/getAllDept")
    @ResponseBody
    public Object getAllDept(){
        List<Dept> deptList = deptService.getAll();
        return deptList;
    }
    @RequestMapping("/addDept")
    public String addDept(Dept dept){//添加部门
        System.out.println("要增加的员工属性：  "+dept);
        Integer result = deptService.insertDept(dept);
        if (result==1){
            return "redirect:/getAllAll";
        }
        return "redirect:/getAllAll";
    }

    @RequestMapping("/delDept")
    @ResponseBody
    public Object delDept(@RequestParam("did")Integer id){//单项删除部门，点击删除按钮，拿到对应的id进行删除
        System.out.println("单项删除部门拿到的id：  "+id);
        Integer result = deptService.deleteDept(id);
        if (result==1){
            return result;
        }
        return "redirect:/getAllAll";
    }

    @RequestMapping("/delSomeDept")
    @ResponseBody
    public Object delSomeDept(String ids){//批量删除--拿到选中的CheckBox--id进行批量删除
        System.out.println("批量删除拿到的id： ---  "+ids);
        Integer result = deptService.deleteSomeDept(ids);
        System.out.println("---------"+result);
        if (result>=1){
            return result;
        }else {
            return "redirect:/getAllAll";
        }
    }
    @RequestMapping("/updateDept")
    @ResponseBody
    public Object updateDept(@RequestParam("did") Integer did){//查找当前部门的所有信息，数据回显，修改功能
        System.out.println("修改部门拿到的id:  -- "+did);
        Dept dept= deptService.findDept_update(did);

        return dept;
    }

    @RequestMapping("/updateDeptReal")
    public String updateDeptReal(Dept dept){//拿到修改后的部门信息，进行修改操作
        System.out.println("修改后的员工信息：  --  "+dept);
        Integer result =deptService.updateDept(dept);
        System.out.println(result+"========================");
        return "redirect:/getAllAll";
    }

}